package com.junzhiit.nengji.api.web.util.rsa;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class AESUtil {

	private static final String ALGO = "AES";
	
	public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
	
	private static final byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };

	public static String encrypt(String Data) throws Exception {
		Key key = generateKey();
		Cipher c = Cipher.getInstance(ALGO);
		c.init(Cipher.ENCRYPT_MODE, key);
		byte[] encVal = c.doFinal(Data.getBytes());
		String encryptedValue = new String(Base64.encodeBase64(encVal));
		return encryptedValue;
	}

	public static String decrypt(String encryptedData) throws Exception {
		Key key = generateKey();
		Cipher c = Cipher.getInstance(ALGO);
		c.init(Cipher.DECRYPT_MODE, key);
		byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
		byte[] decValue = c.doFinal(decordedValue);
		String decryptedValue = new String(decValue);
		return decryptedValue;
	}

	private static Key generateKey() throws Exception {
		Key key = new SecretKeySpec(keyValue, ALGO);
		return key;
	}

	public static String encrypt(String Data, String keystr) throws Exception {
		byte[] keybyte = keystr.getBytes();
		Key key = new SecretKeySpec(keybyte, ALGO);
		Cipher c = Cipher.getInstance(ALGO);
		c.init(Cipher.ENCRYPT_MODE, key);
		byte[] encVal = c.doFinal(Data.getBytes());
		String encryptedValue = new String(Base64.encodeBase64(encVal));
		return encryptedValue;
	}

	public static String decrypt(String encryptedData, String keystr) throws Exception {
		byte[] keybyte = keystr.getBytes();
		Key key = new SecretKeySpec(keybyte, ALGO);
		Cipher c = Cipher.getInstance(ALGO);
		c.init(Cipher.DECRYPT_MODE, key);
		byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
		byte[] decValue = c.doFinal(decordedValue);
		String decryptedValue = new String(decValue);
		return decryptedValue;
	}
	
	public static String decryptFor256(String encryptedData, String keystr) throws Exception {
		byte[] keybyte = keystr.getBytes();
		Key key = new SecretKeySpec(keybyte, ALGO);
		Cipher c = Cipher.getInstance(CIPHER_ALGORITHM);
		c.init(Cipher.DECRYPT_MODE, key);
		byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
		byte[] decValue = c.doFinal(decordedValue);
		String decryptedValue = new String(decValue);
		return decryptedValue;
	}

	public static void main(String[] args) {
		try {
			String pTest = encrypt("734833133@qq.com", "$Uniyep-2015@)!#");
			System.out.println(pTest);
			pTest = URLEncoder.encode(pTest, "UTF-8");
			System.out.println(pTest);
			pTest = URLDecoder.decode(pTest, "UTF-8");
			System.out.println(pTest);
//			http://resetlink.html?token=DG9ERJQ%2FtmZvb0cxdK%2Fvjw%3D%3D	
//			<input hidden="token" value="DG9ERJQ%2FtmZvb0cxdK%2Fvjw%3D%3D">
//			ajax.call("/password/reset",token,passwrod)

			String decryptTest = decrypt(pTest, "$Uniyep-2015@)!#");
			System.out.println(decryptTest);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
